package fafmessenger_server.boundary;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import fafmessenger_server.control.Controller;

public class GUI extends JFrame {

	private Controller controller;
	private Listener listener;
	private static JTextArea console = new JTextArea();
	private JButton btn_start;
	
	public GUI() {
		super("FAFMessenger Server");
		controller = new Controller();
		listener = new Listener(controller);
		
		Container container = getContentPane();
		
		console.setLineWrap(true);
		JScrollPane scrollPane = new JScrollPane(console);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		container.add(scrollPane, BorderLayout.CENTER);
		
		JPanel southPanel = new JPanel();
		southPanel.setLayout(new FlowLayout());
		container.add(southPanel, BorderLayout.SOUTH);
		
		btn_start = new JButton("Start");
		btn_start.addActionListener(new ActionListener() {		
			@Override
			public void actionPerformed(ActionEvent arg0) {
				btn_startClicked();
			}
		});
		southPanel.add(btn_start);
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(400, 300);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
	
	private void btn_startClicked() {
		btn_start.setEnabled(false);
		listener.start();
	}
	
	public static void writeLineToConsole(String message) {
		console.setText(console.getText() + message + "\r\n");
		console.setCaretPosition(console.getDocument().getLength());
	}
}
